// C source file with the function definitions to handle ASCII characters

// check_ascii_int() function
byte check_ascii_int(ascii_int ch)
{
  if ((ch >= MIN_ASCII_INT) && (ch <= MAX_ASCII_INT))
    return 1;
  return 0;
}

// check_enc_ascii() function
byte check_enc_ascii(void * src, umax ssize)
{
  // check function parameters
  if (src == NULL || ssize == 0)
    return 0;
  // else, check the integer (reuse check_ascii_int())
  return check_ascii_int((ascii_int) ((byte *) src)[0]);
}

// get_ascii_int() function
ascii_int get_ascii_int(void * src, umax ssize)
{
  // check the mem rep
  if (check_enc_ascii(src, ssize) == 0)
    return INV_ASCII_INT;
  // else return the int rep
  return (ascii_int) ((byte *) src)[0];
}

// write_enc_ascii() function
void * write_enc_ascii(ascii_int ch, void * dest, umax dsize)
{
  // check the function parameters
  if (check_ascii_int(ch) == 0 || dest == NULL || dsize == 0)
    return NULL;
  // rite dat pice offa ting in
  ((byte *) dest)[0] = (byte) ch;
  return ((byte *) dest + 1);
}

// print_ascii_int() function
byte print_ascii_int(ascii_int ch)
{
  // invalid character
  byte rtn = check_ascii_int(ch);
  if (rtn == 0)
    printf(INV_ASCII_INT_STR);
  else
    printf("%02llX", ch);
  return rtn;
}

// print_enc_ascii() function
byte print_enc_ascii(void * src, umax mx_size)
{
  // invalid character
  byte rtn = check_enc_ascii(src, mx_size);
  if (rtn == 0)
    printf(INV_ENC_ASCII_STR);
  else
    printf("0x%02X", ((byte *) src)[0]);
  return rtn;
}

// check_ascii_as_unicd() function
byte check_ascii_as_unicd(ascii_int ch)
{
  // all characters on ASCII are able to be converted to unicode
  return check_ascii_int(ch);
}

// check_unicd_as_ascii() function
bool check_unicd_as_ascii(unicd_int ch)
{
  // check if it is an ASCII character (0x00 - 0x7F)
  if (ch >= MIN_ASCII_INT && ch <= MAX_ASCII_INT)
    return true;
  return false;
}

// get_ascii_as_unicd() function
unicd_int get_ascii_as_unicd(ascii_int ch)
{
  if (check_ascii_as_unicd(ch) == 0)
    return INV_UNICD_INT;
  return (unicd_int) ch;
}

// get_unicd_as_ascii() function
ascii_int get_unicd_as_ascii(unicd_int ch)
{
  if (check_unicd_as_ascii(ch) == 0)
    return INV_ASCII_INT;
  return (ascii_int) ch;
}
